/*
 * ciruclarbuffer.c
 *
 *  Created on: Jul 30, 2009
 *      Author: z1083248
 */
#include "circularbuffer.h"
#include "num_functions.h"
#include "gen_circularbuffer.h"
#include "pow2_circularbuffer.h"
#include <stdbool.h>

circularBufferT circBuffer;

/*
 * General form of the initialize circular buffer; determines if the size is a
 * power of two and sets the function pointers appropriately.
 */
circularBufferT * initCircularBuffer(BUFF_SIZE size)
{
   // init common members
   circBuffer.isEmpty = true;
   circBuffer.size = size;
   circBuffer.data = (BUFF_DATA *)malloc(sizeof(BUFF_DATA) * size);
   circBuffer.ptr_first = circBuffer.data;
   circBuffer.ptr_end = circBuffer.data;
   circBuffer.fifo_read_index = circBuffer.data;

   if (IsPowerOfTwo(size))
   {
      // point functions to the power_of_two functions
      circBuffer.addToBuffer = pow2_addToBuffer;
      circBuffer.readNext = pow2_readNext;
      circBuffer.printLifoBuffer = pow2_printLifoBuffer;
      circBuffer.dispose = pow2_dispose;
      pow2_initBuffer(&circBuffer);
   }
   else
   {
      // point functions to the general functions
      circBuffer.addToBuffer = gen_addToBuffer;
      circBuffer.readNext = gen_readNext;
      circBuffer.printLifoBuffer = gen_printLifoBuffer;
      circBuffer.dispose = gen_dispose;
      gen_initBuffer(&circBuffer);
   }

   return &circBuffer;
}
